package com.example.easyscript.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.easyscript.dto.Req.AddImageReq;
import com.example.easyscript.entity.task.Image;
import com.example.easyscript.entity.task.Status;
import com.example.easyscript.entity.task.StatusImage;
import com.example.easyscript.service.task.StatusImageService;
import com.example.easyscript.service.task.StatusService;
import com.example.easyscript.service.task.TaskLoader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huzixian
 * @date 2023-03-08
 */
@Api(value = "状态管理", tags = "状态管理")
@Slf4j
@RestController
@RequestMapping(value = "/web/status")
public class StatusController {

    @Resource
    private StatusService statusService;

    @Resource
    private StatusImageService statusImageService;

    @ApiOperation("获取对应任务的状态集")
    @GetMapping("/getList/{taskId}")
    public List<Status> getList(@PathVariable Integer taskId) {
        return TaskLoader.loadEntityListByTaskId(taskId, statusService, Status.class);
    }

    @ApiOperation("新增状态")
    @PostMapping("/save")
    public void save(@RequestBody Status status) {
        Integer order = TaskLoader.getMaxOrder(statusService, status.getTaskId());
        status.setOrderId(order + 1);
        statusService.save(status);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@RequestBody Status status) {
        statusService.updateById(status);
    }

    @ApiOperation("获取状态标志")
    @GetMapping("/getStatusImage/{statusId}")
    public List<Image> getStatusImage(@PathVariable Integer statusId) {
        return statusImageService.getImagesByStatusId(statusId);
    }

    @ApiOperation("新增状态标志")
    @PostMapping("/addStatusImage")
    public void addStatus(@RequestBody AddImageReq statusImageReq) {
        statusImageService.saveStatus(statusImageReq);
    }

    @ApiOperation("删除状态标识")
    @PostMapping("/deleteStatusImage/{statusId}/{imageId}")
    public void deleteStatusImage(@PathVariable String imageId, @PathVariable String statusId) {
        QueryWrapper<StatusImage> wrapper = new QueryWrapper<>();
        wrapper.eq("status_id", statusId);
        wrapper.eq("image_id", imageId);
        statusImageService.remove(wrapper);
    }

}